package com.aurora.listener;

import com.aurora.event.OperationLogEvent;
import com.aurora.spider.site.usatoday.HitProject;
import com.aurora.spider.site.usatoday.UsatodaySearch;
import com.aurora.spider.site.vo.Coupon;
import com.aurora.spider.site.vo.Data;
import com.aurora.spider.site.vo.Page;
import com.aurora.spider.site.vo.SitResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: blog-aurora
 * @description: sit站点监听
 * @author: jonk
 * @create: 2023-09-04 10:20
 **/


@Component
@Log4j2
public class SiteListener {

    @Async
    @EventListener(OperationLogEvent.class)
    public void saveOperationLog(OperationLogEvent operationLogEvent) {

        //组装结果
        SitResult result = new SitResult();
        Data data = new Data();
        Page page = new Page();
        try {
            UsatodaySearch usatodaySearch = (UsatodaySearch) operationLogEvent.getSource();

            //page
            List<Coupon> couponList = new ArrayList<>();
            BeanUtils.copyProperties(usatodaySearch,page);
            data.setPage(page);
            //couponList
            int i =1;
            for (HitProject hitProject : usatodaySearch.getProjectList()) {
                Coupon coupon = new Coupon();
                BeanUtils.copyProperties(hitProject,coupon);
                coupon.setIndex(i++);
            }
            data.setCoupon(couponList);
            result.setData(data);
        }catch (Exception e){
            log.error("MacysSearch error " ,e);
            result.setCode(500);
        }
        //返回结果 mq
        log.info("MacysSearch result {} ",result);

    }

}